home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / winsock / ircii2-6.zip / SRC\IRCII-2.6\SOURCE\WINDOW.C < prev    next >
C/C++ Source or Header  |  1995-01-09  |  59KB  |  2,600 lines

  1. /*
  2.  * window.c: Handles the Main Window stuff for irc.  This includes proper
  3.  * scrolling, saving of screen memory, refreshing, clearing, etc. 
  4.  *
  5.  * Written By Michael Sandrof
  6.  *
  7.  * Copyright(c) 1990 
  8.  *
  9.  * See the COPYRIGHT file, or do a HELP IRCII COPYRIGHT 
  10.  */
  11.  
  12. #ifndef lint
  13. static    char    rcsid[] = "@(#)$Id: window.c,v 1.16 1994/10/09 06:39:35 mrg Stab $";
  14. #endif
  15.  
  16. #include "irc.h"
  17.  
  18. #include "screen.h"
  19. #include "menu.h"
  20. #include "window.h"
  21. #include "vars.h"
  22. #include "server.h"
  23. #include "list.h"
  24. #include "term.h"
  25. #include "names.h"
  26. #include "ircaux.h"
  27. #include "input.h"
  28. #include "status.h"
  29. #include "output.h"
  30. #include "log.h"
  31. #include "hook.h"
  32. #include "dcc.h"
  33. #include "translat.h"
  34.  
  35. /* value for underline mode, is 0 when on!  -lynx */
  36. char    underline = 1;
  37.  
  38. /*
  39.  * The following should synthesize MAXINT on any machine with an 8 bit
  40.  * word.
  41.  */
  42. #define    MAXINT (-1&~(1<<(sizeof(int)*8-1)))
  43.  
  44. Window    *invisible_list = (Window *) 0;
  45.                         /* list of hidden windows */
  46. char    *who_from = (char *) 0;    /* nick of person who's message
  47.                          * is being displayed */
  48. int    who_level = LOG_CRAP;/* Log level of message being displayed */
  49. static    char    *save_from = (char *) 0;
  50. static    int    save_level = LOG_CRAP;
  51.  
  52. int    in_window_command = 0;    /* set to true if we are in window().  This
  53.                  * is used if a put_it() is called within the
  54.                  * window() command.  We make sure all
  55.                  * windows are fully updated before doing the
  56.                  * put_it(). */
  57.  
  58. extern    char    *redirect_nick;
  59.  
  60. /*
  61.  * window_display: this controls the display, 1 being ON, 0 being OFF.  The
  62.  * DISPLAY var sets this. 
  63.  */
  64. unsigned int    window_display = 1;
  65.  
  66. /*
  67.  * status_update_flag: if 1, the status is updated as normal.  If 0, then all
  68.  * status updating is suppressed 
  69.  */
  70.     int    status_update_flag = 1;
  71.  
  72. extern    void    close_all_dcc();
  73. extern    void    close_all_exec();
  74.  
  75. /*
  76.  * traverse_all_windows: This will do as the name implies, traverse every
  77.  * window (visible and invisible) and return a pointer to each window on
  78.  * subsequent calls.  If flag points to a non-zero value, then the traversal
  79.  * in started from the beginning again, and flag is set to point to 0.  This
  80.  * returns all visible windows first, then all invisible windows.  It returns
  81.  * null after all windows have been returned.  It should generally be used as
  82.  * follows: 
  83.  *
  84.  * This *was* like this:
  85.  *
  86.  *   flag = 1; while(tmp = traverse_all_windows(&flag)) { code here } 
  87.  *
  88.  * This means there are static variables, which vary within a loop,
  89.  * which is a breach of the message driven programming model. This
  90.  * is latent under UNIX, but catastrophic under Windows.
  91.  *
  92.  * Changed to:
  93.  *
  94.  *   flag = 1; while (tmp = traverse_all_windows(&flag, &ti) { code here }
  95.  * Major revamp by phone (phone@coombs.anu.edu.au), December 1992.
  96.  */
  97. Window    *
  98. traverse_all_windows(flag, pti)
  99.     int    *flag;
  100.     TravInfo *pti;
  101. {
  102.     int    foo = 1;
  103.  
  104.     /* First call, return the current window basically */
  105.     if (*flag)
  106.     {
  107.         *flag = 0;
  108.         pti->visible = 1;
  109.         if (!screen_list)
  110.             return (Window *) 0;
  111.         pti->screen = screen_list;
  112.         pti->which = pti->screen->window_list;
  113.         if (pti->which)
  114.             return (pti->which);
  115.         else
  116.             foo = 0;
  117.     }
  118.  
  119.     /*
  120.      * foo is used to indicate the the current screen has no windows.
  121.      * This happens when we create a new screen..  either way, we want
  122.      * to go on to the next screen, so if foo isn't set, then if which
  123.      * is already null, return it again (this should never happen, if
  124.      * traverse_all_windows()'s is called properly), else move on to
  125.      * the next window
  126.      */
  127.     if (foo)
  128.         if (!pti->which)
  129.             return (Window *) 0;
  130.         else
  131.             pti->which = pti->which->next;
  132.  
  133.     if (!pti->which)
  134.     {
  135.         while (pti->screen)
  136.         {
  137.             pti->screen = pti->screen->next;
  138.             if (pti->screen && pti->screen->alive)
  139.                 break;
  140.         }
  141.         if (pti->screen)
  142.             pti->which = pti->screen->window_list;
  143.     }
  144.  
  145.     if (pti->which)
  146.         return (pti->which);
  147.     /* 
  148.      * Got to the end of the visible list..  so we do the invisible list..
  149.      * Should also mean, that we've got to the end of all the visible
  150.      * screen..
  151.      */
  152.     if (pti->visible)
  153.     {
  154.         pti->visible = 0;
  155.         pti->which = invisible_list;
  156.         return (pti->which);
  157.     }
  158.     return ((Window *) 0);
  159. }
  160.  
  161. #if 0
  162. /*
  163.  * New version of traverse_all_windows that doesn't require that you don't
  164.  * use it non-recusively.  Pass it a NULL pointer initially, and it will 
  165.  * return 0 when it has finished, and also the address of an int, that is
  166.  * initally 1
  167.  */
  168. static    int
  169. new_window_traverse(window, visible)
  170.     Window    **window;
  171.     int    *visible;
  172. {
  173.     Screen    *screen,
  174.         *old_screen;
  175.  
  176.     if (!*window)
  177.     {
  178.         if (!(screen = screen_list))
  179.         {
  180.             *visible = 0;
  181.             if (!invisible_list)
  182.                 return 0;
  183.             *window = invisible_list;
  184.             return 1;
  185.         }
  186.         for (;screen && !screen->alive; screen = screen->next)
  187.             ;
  188.         if (!screen)
  189.         {
  190.             *visible = 0;
  191.             if (!invisible_list)
  192.                 return 0;
  193.             *window = invisible_list;
  194.             return 1;
  195.         }
  196.     }
  197.     else
  198.     {
  199.         if ((*window)->next)
  200.         {
  201.             *window = (*window)->next;
  202.             return 1;
  203.         }
  204.         if (!*visible)
  205.             return 0;
  206.         for (old_screen = screen = (*window)->screen;
  207.                 screen && !screen->alive; screen = screen->next)
  208.             ;
  209.         if (!screen)
  210.         {
  211.             *visible = 0;
  212.             if (!invisible_list)
  213.                 return 0;
  214.             *window = invisible_list;
  215.             return 1;
  216.         }
  217.         if (screen == old_screen)
  218.             return 0;
  219.     }
  220.     *window = screen->current_window;
  221.     return (*window) ? 1 : 0;
  222. }
  223. #endif
  224.  
  225.  
  226. /*
  227.  * set_scroll_lines: called by /SET SCROLL_LINES to check the scroll lines
  228.  * value 
  229.  */
  230. void
  231. set_scroll_lines(size)
  232.     int    size;
  233. {
  234.     if (size == 0)
  235.     {
  236.         set_var_value(SCROLL_VAR, var_settings[0]);
  237.         if (curr_scr_win)
  238.             curr_scr_win->scroll = 0;
  239.     }
  240.     else if (size > curr_scr_win->display_size)
  241.     {
  242.         say("Maximum lines that may be scrolled is %d", 
  243.             curr_scr_win->display_size);
  244.         set_int_var(SCROLL_LINES_VAR, curr_scr_win->display_size);
  245.     }
  246. }
  247.  
  248. /*
  249.  * set_scroll: called by /SET SCROLL to make sure the SCROLL_LINES variable
  250.  * is set correctly 
  251.  */
  252. void
  253. set_scroll(value)
  254.     int    value;
  255. {
  256.     int    old_value;
  257.  
  258.     if (value && (get_int_var(SCROLL_LINES_VAR) == 0))
  259.     {
  260.         put_it("You must set SCROLL_LINES to a positive value first!");
  261.         if (curr_scr_win)
  262.             curr_scr_win->scroll = 0;
  263.     }
  264.     else
  265.     {
  266.         if (curr_scr_win)
  267.         {
  268.             old_value = curr_scr_win->scroll;
  269.             curr_scr_win->scroll = value;
  270.             if (old_value && !value)
  271.                 scroll_window(curr_scr_win);
  272.         }
  273.     }
  274. }
  275.  
  276. /*
  277.  * reset_line_cnt: called by /SET HOLD_MODE to reset the line counter so we
  278.  * always get a held screen after the proper number of lines 
  279.  */
  280. void
  281. reset_line_cnt(value)
  282.     int    value;
  283. {
  284.     curr_scr_win->hold_mode = value;
  285.     curr_scr_win->hold_on_next_rite = 0;
  286.     curr_scr_win->line_cnt = 0;
  287. }
  288.  
  289. /*
  290.  * set_continued_line: checks the value of CONTINUED_LINE for validity,
  291.  * altering it if its no good 
  292.  */
  293. void
  294. set_continued_line(value)
  295.     char    *value;
  296. {
  297.     if (value && (strlen(value) > (CO / 2)))
  298.         value[CO / 2] = '\0';
  299. }
  300.  
  301. /*
  302.  * free_hold: This frees all the data and structures associated with the hold
  303.  * list for the given window 
  304.  */
  305. static    void
  306. free_hold(window)
  307.     Window    *window;
  308. {
  309.     Hold *tmp,
  310.         *next;
  311.  
  312.     for (tmp = window->hold_head; tmp; tmp = next)
  313.     {
  314.         next = tmp->next;
  315.         new_free(&(tmp->str));
  316.         new_free(&tmp);
  317.     }
  318. }
  319.  
  320. /*
  321.  * free_lastlog: This frees all data and structures associated with the
  322.  * lastlog of the given window 
  323.  */
  324. static    void
  325. free_lastlog(window)
  326.     Window    *window;
  327. {
  328.     Lastlog *tmp,
  329.         *next;
  330.  
  331.     for (tmp = window->lastlog_head; tmp; tmp = next)
  332.     {
  333.         next = tmp->next;
  334.         new_free(&(tmp->msg));
  335.         new_free(&tmp);
  336.     }
  337. }
  338.  
  339. /*
  340.  * free_display: This frees all memory for the display list for a given
  341.  * window.  It resets all of the structures related to the display list
  342.  * appropriately as well 
  343.  */
  344. static    void
  345. free_display(window)
  346.     Window    *window;
  347. {
  348.     Display *tmp,
  349.         *next;
  350.     int    i;
  351.  
  352.     if (window == (Window *) 0)
  353.         window = curr_scr_win;
  354.     for (tmp = window->top_of_display, i = 0; i < window->display_size; i++, tmp = next)
  355.     {
  356.         next = tmp->next;
  357.         new_free(&(tmp->line));
  358.         new_free(&tmp);
  359.     }
  360.     window->top_of_display = (Display *) 0;
  361.     window->display_ip = (Display *) 0;
  362.     window->display_size = 0;
  363. }
  364.  
  365. static    void
  366. free_nicks(window)
  367.     Window    *window;
  368. {
  369.     NickList *tmp,
  370.         *next;
  371.  
  372.     for (tmp = window->nicks; tmp; tmp = next)
  373.     {
  374.         next = tmp->next;
  375.         new_free(&(tmp->nick));
  376.         new_free(&tmp);
  377.     }
  378. }
  379.  
  380. /*
  381.  * erase_display: This effectively causes all members of the display list for
  382.  * a window to be set to empty strings, thus "clearing" a window.  It sets
  383.  * the cursor to the top of the window, and the display insertion point to
  384.  * the top of the display. Note, this doesn't actually refresh the screen,
  385.  * just cleans out the display list 
  386.  */
  387. void
  388. erase_display(window)
  389.     Window    *window;
  390. {
  391.     int    i;
  392.     Display *tmp;
  393.  
  394.     if (dumb)
  395.         return;
  396.     if (window == (Window *) 0)
  397.         window = curr_scr_win;
  398.     for (tmp = window->top_of_display, i = 0; i < window->display_size;
  399.             i++, tmp = tmp->next)
  400.         new_free(&(tmp->line));
  401.     window->cursor = 0;
  402.     window->line_cnt = 0;
  403.     window->hold_on_next_rite = 0;
  404.     window->display_ip = window->top_of_display;
  405. }
  406.  
  407. static    void
  408. remove_from_invisible_list(window)
  409.     Window    *window;
  410. {
  411.     window->visible = 1;
  412.     window->screen = current_screen;
  413.     window->miscflags &= ~WINDOW_NOTIFIED;
  414.     if (window->prev)
  415.         window->prev->next = window->next;
  416.     else
  417.         invisible_list = window->next;
  418.     if (window->next)
  419.         window->next->prev = window->prev;
  420. }
  421.  
  422. extern    void
  423. add_to_invisible_list(window)
  424.     Window    *window;
  425. {
  426.     if ((window->next = invisible_list) != NULL)
  427.         invisible_list->prev = window;
  428.     invisible_list = window;
  429.     window->prev = (Window *) 0;
  430.     window->visible = 0;
  431.     window->screen = (Screen *) 0;
  432. }
  433.  
  434. /*
  435.  * swap_window: This swaps the given window with the current window.  The
  436.  * window passed must be invisible.  Swapping retains the positions of both
  437.  * windows in their respective window lists, and retains the dimensions of
  438.  * the windows as well 
  439.  */
  440. static    void
  441. swap_window(v_window, window)
  442.     Window    *v_window;
  443.     Window    *window;
  444. {
  445.     Window tmp, *prev, *next;
  446.     int    top, bottom, size;
  447.  
  448.     if (window->visible || !v_window->visible)
  449.     {
  450.         say("You can only SWAP a hidden window with a visible window.");
  451.         return;
  452.     }
  453.     prev = v_window->prev;
  454.     next = v_window->next;
  455.  
  456.     current_screen->last_window_refnum = v_window->refnum;
  457.     current_screen->last_window_refnum = v_window->refnum;
  458.     remove_from_invisible_list(window);
  459.  
  460.     tmp = *v_window;
  461.     *v_window = *window;
  462.     v_window->top = tmp.top;
  463.     v_window->bottom = tmp.bottom;
  464.     v_window->display_size = tmp.display_size + tmp.menu.lines -
  465. #ifdef SCROLL_AFTER_DISPLAY
  466.         v_window->menu.lines - 1;
  467. #else
  468.         v_window->menu.lines;
  469. #endif /* SCROLL_AFTER_DISPLAY */
  470.     v_window->prev = prev;
  471.     v_window->next = next;
  472.  
  473.     top = window->top;
  474.     bottom = window->bottom;
  475.     size = window->display_size;
  476.     *window = tmp;
  477.     window->top = top;
  478.     window->bottom = bottom;
  479. #ifdef SCROLL_AFTER_DISPLAY
  480.     window->display_size = size - 1;
  481. #else
  482.     window->display_size = size;
  483. #endif /* SCROLL_AFTER_DISPLAY */
  484.  
  485.     add_to_invisible_list(window);
  486.  
  487.     v_window->update |= REDRAW_DISPLAY_FULL | REDRAW_STATUS;
  488.     window->update |= REDRAW_DISPLAY_FULL | REDRAW_STATUS;
  489. }
  490.  
  491. /*
  492.  * move_window: This moves a window offset positions in the window list. This
  493.  * means, of course, that the window will move on the screen as well 
  494.  */
  495. static    void
  496. move_window(window, offset)
  497.     Window    *window;
  498.     int    offset;
  499. {
  500.     Window    *tmp,
  501.         *last;
  502.     int    win_pos,
  503.     pos;
  504.  
  505.     if (offset == 0)
  506.         return;
  507.     last = (Window *) 0;
  508.     for (win_pos = 0, tmp = current_screen->window_list; tmp;
  509.         tmp = tmp->next, win_pos++)
  510.     {
  511.         if (window == tmp)
  512.             break;
  513.         last = tmp;
  514.     }
  515.     if (tmp == (Window *) 0)
  516.         return;
  517.     if (last == (Window *) 0)
  518.         current_screen->window_list = tmp->next;
  519.     else
  520.         last->next = tmp->next;
  521.     if (tmp->next)
  522.         tmp->next->prev = last;
  523.     else
  524.         current_screen->window_list_end = last;
  525.     if (offset < 0)
  526.         win_pos = (current_screen->visible_windows + offset + win_pos) %
  527.             current_screen->visible_windows;
  528.     else
  529.         win_pos = (offset + win_pos) % current_screen->visible_windows;
  530.     last = (Window *) 0;
  531.     for (pos = 0, tmp = current_screen->window_list;
  532.         pos != win_pos; tmp = tmp->next, pos++)
  533.         last = tmp;
  534.     if (last == (Window *) 0)
  535.         current_screen->window_list = window;
  536.     else
  537.         last->next = window;
  538.     if (tmp)
  539.         tmp->prev = window;
  540.     else
  541.         current_screen->window_list_end = window;
  542.     window->prev = last;
  543.     window->next = tmp;
  544.     recalculate_window_positions();
  545. }
  546.  
  547. /*
  548.  * grow_window: This will increase or descrease the size of the given window
  549.  * by offset lines (positive offset increases, negative decreases).
  550.  * Obviously, with a fixed terminal size, this means that some other window
  551.  * is going to have to change size as well.  Normally, this is the next
  552.  * window in the window list (the window below the one being changed) unless
  553.  * the window is the last in the window list, then the previous window is
  554.  * changed as well 
  555.  */
  556. static    void
  557. grow_window(window, offset)
  558.     Window    *window;
  559.     int    offset;
  560. {
  561.     Window    *other,
  562.         *tmp;
  563.     int    after,
  564.     window_size,
  565.     other_size;
  566.  
  567.     if (window == (Window *) 0)
  568.         window = curr_scr_win;
  569.     if (!window->visible)
  570.     {
  571.         say("You cannot change the size of hidden windows!");
  572.         return;
  573.     }
  574.     if (window->next)
  575.     {
  576.         other = window->next;
  577.         after = 1;
  578.     }
  579.     else
  580.     {
  581.         other = (Window *) 0;
  582.         for (tmp = current_screen->window_list; tmp; tmp = tmp->next)
  583.         {
  584.             if (tmp == window)
  585.                 break;
  586.             other = tmp;
  587.         }
  588.         if (other == (Window *) 0)
  589.         {
  590.             say("Can't change the size of this window!");
  591.             return;
  592.         }
  593.         after = 0;
  594.     }
  595.     window_size = window->display_size + offset;
  596.     other_size = other->display_size - offset;
  597.     if ((window_size < 3) ||
  598.         (other_size < 3))
  599.     {
  600.         say("Not enough room to resize this window!");
  601.         return;
  602.     }
  603.     if (after)
  604.     {
  605.         window->bottom += offset;
  606.         other->top += offset;
  607.     }
  608.     else
  609.     {
  610.         window->top -= offset;
  611.         other->bottom -= offset;
  612.     }
  613. #ifdef SCROLL_AFTER_DISPLAY
  614.     window->display_size = window_size - 1;
  615.     other->display_size = other_size - 1;
  616. #else
  617.     window->display_size = window_size;
  618.     other->display_size = other_size;
  619. #endif /* SCROLL_AFTER_DISPLAY */
  620.     window->update |= REDRAW_DISPLAY_FULL | REDRAW_STATUS;
  621.     other->update |= REDRAW_DISPLAY_FULL | REDRAW_STATUS;
  622.     term_flush();
  623. }
  624.  
  625.  
  626. /*
  627.  * save_message_from: this is used to save (for later restoration) the
  628.  * who_from variable.  This comes in handy very often when a routine might
  629.  * call another routine that might change who_from. 
  630.  */
  631. void
  632. save_message_from()
  633. {
  634.     malloc_strcpy(&save_from, who_from);
  635.     save_level = who_level;
  636. }
  637.  
  638. /* restore_message_from: restores a previously saved who_from variable */
  639. void
  640. restore_message_from()
  641. {
  642.     malloc_strcpy(&who_from, save_from);
  643.     who_level = save_level;
  644. }
  645.  
  646. /*
  647.  * message_from: With this you can the who_from variable and the who_level
  648.  * variable, used by the display routines to decide which window messages
  649.  * should go to.  
  650.  */
  651. void
  652. message_from(who, level)
  653.     char    *who;
  654.     int    level;
  655. {
  656.     malloc_strcpy(&who_from, who);
  657.     who_level = level;
  658. }
  659.  
  660. /*
  661.  * message_from_level: Like set_lastlog_msg_level, except for message_from.
  662.  * this is needed by XECHO, because we could want to output things in more
  663.  * than one level.
  664.  */
  665. int
  666. message_from_level(level)
  667.     int    level;
  668. {
  669.     int    temp;
  670.  
  671.     temp = who_level;
  672.     who_level = level;
  673.     return temp;
  674. }
  675.  
  676. /*
  677.  * get_window_by_refnum: Given a reference number to a window, this returns a
  678.  * pointer to that window if a window exists with that refnum, null is
  679.  * returned otherwise.  The "safe" way to reference a window is throught the
  680.  * refnum, since a window might be delete behind your back and and Window
  681.  * pointers might become invalid.  
  682.  */
  683. Window    *
  684. get_window_by_refnum(refnum)
  685.     u_int    refnum;
  686. {
  687.     Window    *tmp;
  688.     int    flag = 1;
  689.     TravInfo ti;
  690.  
  691.     if (refnum)
  692.     {
  693.         while ((tmp = traverse_all_windows(&flag, &ti)) != NULL)
  694.         {
  695.             if (tmp->refnum == refnum)
  696.                 return (tmp);
  697.         }
  698.     }
  699.     else
  700.         return (curr_scr_win);
  701.     return ((Window *) 0);
  702. }
  703.  
  704. /*
  705.  * clear_window_by_refnum: just like clear_window(), but it uses a refnum. If
  706.  * the refnum is invalid, the current window is cleared. 
  707.  */
  708. void
  709. clear_window_by_refnum(refnum)
  710.     u_int    refnum;
  711. {
  712.     Window    *tmp;
  713.  
  714.     if ((tmp = get_window_by_refnum(refnum)) == (Window *) 0)
  715.         tmp = curr_scr_win;
  716.     clear_window(tmp);
  717. }
  718.  
  719. /*
  720.  * revamp_window_levels: Given a level setting for the current window, this
  721.  * makes sure that that level setting is unused by any other window. Thus
  722.  * only one window in the system can be set to a given level.  This only
  723.  * revamps levels for windows with servers matching the given window 
  724.  * it also makes sure that only one window has the level `DCC', as this is
  725.  * not dependant on a server.
  726.  */
  727. static    void
  728. revamp_window_levels(window)
  729.     Window    *window;
  730. {
  731.     Window    *tmp;
  732.     int    flag = 1;
  733.     int    got_dcc;
  734.     TravInfo ti;
  735.  
  736.     got_dcc = (LOG_DCC & window->window_level) ? 1 : 0;
  737.     while ((tmp = traverse_all_windows(&flag, &ti)) != NULL)
  738.     {
  739.         if (tmp == window)
  740.             continue;
  741.         if (LOG_DCC & tmp->window_level)
  742.         {
  743.             if (0 != got_dcc)
  744.                 tmp->window_level &= ~LOG_DCC;
  745.             got_dcc = 1;
  746.         }
  747.         if (window->server == tmp->server)
  748.             tmp->window_level ^= (tmp->window_level & window->window_level);
  749.     }
  750. }
  751.  
  752. /*
  753.  * set_level_by_refnum: This sets the window level given a refnum.  It
  754.  * revamps the windows levels as well using revamp_window_levels() 
  755.  */
  756. void
  757. set_level_by_refnum(refnum, level)
  758.     u_int    refnum;
  759.     int    level;
  760. {
  761.     Window    *tmp;
  762.  
  763.     if ((tmp = get_window_by_refnum(refnum)) == (Window *) 0)
  764.         tmp = curr_scr_win;
  765.     tmp->window_level = level;
  766.     revamp_window_levels(tmp);
  767. }
  768.  
  769. /*
  770.  * set_prompt_by_refnum: changes the prompt for the given window.  A window
  771.  * prompt will be used as the target in place of the query user or current
  772.  * channel if it is set 
  773.  */
  774. void
  775. set_prompt_by_refnum(refnum, prompt)
  776.     u_int    refnum;
  777.     char    *prompt;
  778. {
  779.     Window    *tmp;
  780.  
  781.     if ((tmp = get_window_by_refnum(refnum)) == (Window *) 0)
  782.         tmp = curr_scr_win;
  783.     malloc_strcpy(&(tmp->prompt), prompt);
  784. }
  785.  
  786. /*
  787.  * message_to: This allows you to specify a window (by refnum) as a
  788.  * destination for messages.  Used by EXEC routines quite nicely 
  789.  */
  790. void
  791. message_to(refnum)
  792.     u_int    refnum;
  793. {
  794.     if (refnum)
  795.         to_window = get_window_by_refnum(refnum);
  796.     else
  797.         to_window = (Window *) NULL;
  798. }
  799.  
  800. /*
  801.  * get_next_window: This returns a pointer to the next *visible* window in
  802.  * the window list.  It automatically wraps at the end of the list back to
  803.  * the beginning of the list 
  804.  */
  805. static    Window    *
  806. get_next_window()
  807. {
  808.     if (curr_scr_win && curr_scr_win->next)
  809.         return (curr_scr_win->next);
  810.     else
  811.         return (current_screen->window_list);
  812. }
  813.  
  814. /*
  815.  * get_previous_window: this returns the previous *visible* window in the
  816.  * window list.  This automatically wraps to the last window in the window
  817.  * list 
  818.  */
  819. static    Window    *
  820. get_previous_window()
  821. {
  822.     if (curr_scr_win && curr_scr_win->prev)
  823.         return (curr_scr_win->prev);
  824.     else
  825.         return (current_screen->window_list_end);
  826. }
  827.  
  828. /*
  829.  * set_current_window: This sets the "current" window to window.  It also
  830.  * keeps track of the last_current_screen->current_window by setting it to the
  831.  * previous current window.  This assures you that the new current window is
  832.  * visible.
  833.  * If not, a new current window is chosen from the window list 
  834.  */
  835. void
  836. set_current_window(window)
  837.     Window    *window;
  838. {
  839.     Window    *tmp;
  840.     unsigned int    refnum;
  841.  
  842.     refnum = current_screen->last_window_refnum;
  843.     if (curr_scr_win)
  844.  
  845.     {
  846.         curr_scr_win->update |= UPDATE_STATUS;
  847.         current_screen->last_window_refnum = curr_scr_win->refnum;
  848.     }
  849.     if ((window == (Window *) 0) || (!window->visible))
  850.  
  851.     {
  852.         if ((tmp = get_window_by_refnum(refnum)) && (tmp->visible))
  853.             curr_scr_win = tmp;
  854.         else
  855.             curr_scr_win = get_next_window();
  856.     }
  857.     else
  858.         curr_scr_win = window;
  859.     curr_scr_win->update |= UPDATE_STATUS;
  860. }
  861.  
  862. /*
  863.  * swap_last_window:  This swaps the current window with the last window
  864.  * that was hidden.
  865.  */
  866.  
  867. void
  868. swap_last_window()
  869. {
  870.     if (invisible_list == (Window *) 0)
  871.     {
  872.         /* say("There are no hidden windows"); */
  873.         /* Not sure if we need to warn   - phone. */
  874.         return;
  875.     }
  876.     swap_window(curr_scr_win, invisible_list);
  877.     message_from((char *) 0, LOG_CRAP);
  878.     update_all_windows();
  879.     cursor_to_input();
  880. }
  881.  
  882. /*
  883.  * next_window: This switches the current window to the next visible window 
  884.  */
  885. void
  886. next_window()
  887. {
  888.     if (current_screen->visible_windows == 1)
  889.         return;
  890.     set_current_window(get_next_window());
  891.     update_all_windows();
  892. }
  893.  
  894. /*
  895.  * swap_next_window:  This swaps the current window with the next hidden
  896.  * window.
  897.  */
  898.  
  899. void
  900. swap_next_window()
  901. {
  902.     int    flag;
  903.     Window    *tmp;
  904.     int    next = MAXINT;
  905.     int    smallest;
  906.     TravInfo ti;
  907.  
  908.     if (invisible_list == (Window *) 0)
  909.     {
  910.         say("There are no hidden windows");
  911.         return;
  912.     }
  913.     flag = 1;
  914.     smallest = curr_scr_win->refnum;
  915.     while ((tmp = traverse_all_windows(&flag, &ti)) != NULL)
  916.     {
  917.         if (!tmp->visible)
  918.         {
  919.             if (tmp->refnum < smallest)
  920.                 smallest = tmp->refnum;
  921.             if ((tmp->refnum > curr_scr_win->refnum)
  922.                 && (next > tmp->refnum))
  923.                 next = tmp->refnum;
  924.         }
  925.     }
  926.     if (next != MAXINT)
  927.         tmp = get_window_by_refnum(next);
  928.     else
  929.         tmp = get_window_by_refnum(smallest);
  930.     swap_window(curr_scr_win, tmp);
  931.     message_from((char *) 0, LOG_CRAP);
  932.     update_all_windows();
  933.     update_all_status();
  934.     cursor_to_input();
  935. }
  936.  
  937. /*
  938.  * previous_window: This switches the current window to the previous visible
  939.  * window 
  940.  */
  941. void
  942. previous_window()
  943. {
  944.     if (current_screen->visible_windows == 1)
  945.         return;
  946.     set_current_window(get_previous_window());
  947.     update_all_windows();
  948. }
  949.  
  950. /*
  951.  * swap_previous_window:  This swaps the current window with the next 
  952.  * hidden window.
  953.  */
  954.  
  955. void
  956. swap_previous_window()
  957. {
  958.     int    flag;
  959.     Window    *tmp;
  960.     int    previous = 0;
  961.     int    largest;
  962.     TravInfo ti;
  963.  
  964.     if (invisible_list == (Window *) 0)
  965.     {
  966.         say("There are no hidden windows");
  967.         return;
  968.     }
  969.     flag = 1;
  970.     largest = curr_scr_win->refnum;
  971.     while ((tmp = traverse_all_windows(&flag, &ti)) != NULL)
  972.     {
  973.         if (!tmp->visible)
  974.         {
  975.             if (tmp->refnum > largest)
  976.                 largest = tmp->refnum;
  977.             if ((tmp->refnum < curr_scr_win->refnum)
  978.                 && (previous < tmp->refnum))
  979.                 previous = tmp->refnum;
  980.         }
  981.     }
  982.     if (previous)
  983.         tmp = get_window_by_refnum(previous);
  984.     else
  985.         tmp = get_window_by_refnum(largest);
  986.     swap_window(curr_scr_win,tmp);
  987.     message_from((char *) 0, LOG_CRAP);
  988.     update_all_windows();
  989.     update_all_status();
  990.     cursor_to_input();
  991. }
  992.  
  993. /*
  994.  * back_window:  goes to the last window that was current.  Swapping the
  995.  * current window if the last window was hidden.
  996.  */
  997.  
  998. void
  999. back_window()
  1000. {
  1001.     Window    *tmp;
  1002.  
  1003.     tmp = get_window_by_refnum(current_screen->last_window_refnum);
  1004.     if (tmp->visible)
  1005.         set_current_window(tmp);
  1006.     else
  1007.     {
  1008.         swap_window(curr_scr_win, tmp);
  1009.         message_from((char *) 0, LOG_CRAP);
  1010.         update_all_windows();
  1011.         update_all_status();
  1012.         cursor_to_input();
  1013.     }
  1014. }
  1015.  
  1016. /*
  1017.  * add_to_window_list: This inserts the given window into the visible window
  1018.  * list (and thus adds it to the displayed windows on the screen).  The
  1019.  * window is added by splitting the current window.  If the current window is
  1020.  * too small, the next largest window is used.  The added window is returned
  1021.  * as the function value or null is returned if the window couldn't be added 
  1022.  */
  1023. extern    Window    *
  1024. add_to_window_list(new)
  1025.     Window    *new;
  1026. {
  1027.     Window    *biggest = (Window *) 0,
  1028.         *tmp;
  1029.  
  1030.     current_screen->visible_windows++;
  1031.     if (curr_scr_win == (Window *) 0)
  1032.     {
  1033.         current_screen->window_list_end =
  1034.                 current_screen->window_list = new;
  1035.         if (dumb)
  1036.         {
  1037. #ifdef SCROLL_AFTER_DISPLAY
  1038.             new->display_size = 24 - 1;
  1039. #else
  1040.             new->display_size = 24;    /* what the hell */
  1041. #endif /* SCROLL_AFTER_DISPLAY */
  1042.             set_current_window(new);
  1043.             return (new);
  1044.         }
  1045.         recalculate_windows();
  1046.     }
  1047.     else
  1048.     {
  1049.         /* split current window, or find a better window to split */
  1050.         if ((curr_scr_win->display_size < 4) ||
  1051.                 get_int_var(ALWAYS_SPLIT_BIGGEST_VAR))
  1052.         {
  1053.             int    size = 0;
  1054.  
  1055.             for (tmp = current_screen->window_list; tmp;
  1056.                     tmp = tmp->next)
  1057.             {
  1058.                 if (tmp->display_size > size)
  1059.                 {
  1060.                     size = tmp->display_size;
  1061.                     biggest = tmp;
  1062.                 }
  1063.             }
  1064.             if ((biggest == (Window *) 0) || (size < 4))
  1065.             {
  1066.                 say("Not enough room for another window!");
  1067.                 /* Probably a source of memory leaks */
  1068.                 new_free(&new);
  1069.                 current_screen->visible_windows--;
  1070.                 return ((Window *) 0);
  1071.             }
  1072.         }
  1073.         else
  1074.             biggest = curr_scr_win;
  1075.         if ((new->prev = biggest->prev) != NULL)
  1076.             new->prev->next = new;
  1077.         else
  1078.             current_screen->window_list = new;
  1079.         new->next = biggest;
  1080.         biggest->prev = new;
  1081.         new->top = biggest->top;
  1082.         new->bottom = (biggest->top + biggest->bottom) / 2;
  1083.         biggest->top = new->bottom + 1;
  1084. #ifdef SCROLL_AFTER_DISPLAY
  1085.         new->display_size = new->bottom - new->top - 1;
  1086.         biggest->display_size = biggest->bottom - biggest->top -
  1087.             biggest->menu.lines - 1;
  1088. #else
  1089.         new->display_size = new->bottom - new->top;
  1090.         biggest->display_size = biggest->bottom - biggest->top -
  1091.             biggest->menu.lines;
  1092. #endif /* SCROLL_AFTER_DISPLAY */
  1093.         new->update |= REDRAW_DISPLAY_FULL | REDRAW_STATUS;
  1094.         biggest->update |= REDRAW_DISPLAY_FULL | REDRAW_STATUS;
  1095.     }
  1096.     return (new);
  1097. }
  1098.  
  1099. /*
  1100.  * remove_from_window_list: this removes the given window from the list of
  1101.  * visible windows.  It closes up the hole created by the windows abnsense in
  1102.  * a nice way 
  1103.  */
  1104. void
  1105. remove_from_window_list(window)
  1106.     Window    *window;
  1107. {
  1108.     Window    *other;
  1109.  
  1110.     /* find adjacent visible window to close up the screen */
  1111.     for (other = window->next; other; other = other->next)
  1112.     {
  1113.         if (other->visible)
  1114.         {
  1115.             other->top = window->top;
  1116.             break;
  1117.         }
  1118.     }
  1119.     if (other == (Window *) 0)
  1120.     {
  1121.         for (other = window->prev; other; other = other->prev)
  1122.         {
  1123.             if (other->visible)
  1124.             {
  1125.                 other->bottom = window->bottom;
  1126.                 break;
  1127.             }
  1128.         }
  1129.     }
  1130.     /* remove window from window list */
  1131.     if (window->prev)
  1132.         window->prev->next = window->next;
  1133.     else
  1134.         current_screen->window_list = window->next;
  1135.     if (window->next)
  1136.         window->next->prev = window->prev;
  1137.     else
  1138.         current_screen->window_list_end = window->prev;
  1139.     if (window->visible)
  1140.     {
  1141.         current_screen->visible_windows--;
  1142. #ifdef SCROLL_AFTER_DISPLAY
  1143.         other->display_size = other->bottom - other->top -
  1144.             other->menu.lines - 1;
  1145. #else
  1146.         other->display_size = other->bottom - other->top -
  1147.             other->menu.lines;
  1148. #endif /* SCROLL_AFTER_DISPLAY */
  1149.         if (window == curr_scr_win)
  1150.             set_current_window((Window *) 0);
  1151.         if (window->refnum == current_screen->last_window_refnum)
  1152.             current_screen->last_window_refnum =
  1153.                 curr_scr_win->refnum;
  1154.     }
  1155. }
  1156.  
  1157. /*
  1158.  * window_check_servers: this checks the validity of the open servers vs the
  1159.  * current window list.  Every open server must have at least one window
  1160.  * associated with it.  If a window is associated with a server that's no
  1161.  * longer open, that window's server is set to the primary server.  If an
  1162.  * open server has no assicatiate windows, that server is closed.  If the
  1163.  * primary server is no more, a new primary server is picked from the open
  1164.  * servers 
  1165.  */
  1166. void
  1167. window_check_servers()
  1168. {
  1169.     Window    *tmp;
  1170.     int    flag, cnt, max, i, not_connected,
  1171.     prime = -1;
  1172.     TravInfo ti;
  1173.  
  1174.     connected_to_server = 0;
  1175.     max = server_list_size();
  1176.     for (i = 0; i < max; i++)
  1177.     {
  1178.         not_connected = !is_server_open(i);
  1179.         flag = 1;
  1180.         cnt = 0;
  1181.         while ((tmp = traverse_all_windows(&flag, &ti)) != NULL)
  1182.         {
  1183.             if (tmp->server == i)
  1184.             {
  1185.                 if (not_connected)
  1186.                     tmp->server = primary_server;
  1187.                 else
  1188.                     prime = tmp->server;
  1189.                 cnt++;
  1190.             }
  1191.         }
  1192.         if (cnt == 0)
  1193.             close_server(i, "no windows left");
  1194.         else
  1195.             connected_to_server++;
  1196.     }
  1197.     if (!is_server_open(primary_server))
  1198.         primary_server = prime;
  1199.     update_all_status();
  1200.     cursor_to_input();
  1201. }
  1202.  
  1203. /*
  1204.  * delete_window: This deletes the given window.  It frees all data and
  1205.  * structures associated with the window, and it adjusts the other windows so
  1206.  * they will display correctly on the screen. 
  1207.  */
  1208. void
  1209. delete_window(window)
  1210.     Window    *window;
  1211. {
  1212.     char    *tmp = (char *) 0;
  1213.  
  1214.     if (window == (Window *) 0)
  1215.         window = curr_scr_win;
  1216.     if (window->visible && (current_screen->visible_windows == 1))
  1217.     {
  1218.         if (invisible_list)
  1219.         {
  1220.             swap_window(window, invisible_list);
  1221.             window = invisible_list;
  1222.         }
  1223.         else
  1224.         {
  1225.             say("You can't kill the last window!");
  1226.             return;
  1227.         }
  1228.     }
  1229.     if (window->name)
  1230.         strmcpy(buffer, window->name, BIG_BUFFER_SIZE);
  1231.     else
  1232.         sprintf(buffer, "%u", window->refnum);
  1233.     malloc_strcpy(&tmp, buffer);
  1234.     new_free(&(window->status_line));
  1235.     new_free(&(window->query_nick));
  1236.     new_free(&(window->current_channel));
  1237.     new_free(&(window->waiting_channel));
  1238.     new_free(&(window->logfile));
  1239.     new_free(&(window->name));
  1240.     free_display(window);
  1241.     free_hold(window);
  1242.     free_lastlog(window);
  1243.     free_nicks(window);
  1244.     if (window->visible)
  1245.         remove_from_window_list(window);
  1246.     else
  1247.         remove_from_invisible_list(window);
  1248.     new_free(&window);
  1249.     window_check_servers();
  1250.     do_hook(WINDOW_KILL_LIST, "%s", tmp);
  1251.     new_free(&tmp);
  1252. }
  1253.  
  1254. /* delete_other_windows: zaps all visible windows except the current one */
  1255. static    void
  1256. delete_other_windows()
  1257. {
  1258.     Window    *tmp,
  1259.         *cur,
  1260.         *next;
  1261.  
  1262.     cur = curr_scr_win;
  1263.     tmp = current_screen->window_list;
  1264.     while (tmp)
  1265.     {
  1266.         next = tmp->next;
  1267.         if (tmp != cur)
  1268.             delete_window(tmp);
  1269.         tmp = next;
  1270.     }
  1271. }
  1272.  
  1273. /*
  1274.  * window_kill_swap:  Swaps with the last window that was hidden, then
  1275.  * kills the window that was swapped.  Give the effect of replacing the
  1276.  * current window with the last one, and removing it at the same time.
  1277.  */
  1278.  
  1279. void
  1280. window_kill_swap()
  1281. {
  1282.     if (invisible_list != (Window *) 0)
  1283.     {
  1284.         swap_last_window();
  1285.         delete_window(get_window_by_refnum(current_screen->last_window_refnum));
  1286.     }
  1287.     else
  1288.         say("There are no hidden windows!");
  1289. }
  1290.  
  1291. /*
  1292.  * unhold_windows: This is used by the main io loop to display held
  1293.  * information at an appropriate time.  Each time this is called, each
  1294.  * windows hold list is checked.  If there is info in the hold list and the
  1295.  * window is not held, the first line is displayed and removed from the hold
  1296.  * list.  Zero is returned if no infomation is displayed 
  1297.  */
  1298. int
  1299. unhold_windows()
  1300. {
  1301.     Window    *tmp;
  1302.     char    *stuff;
  1303.     int    hold_flag = 0,
  1304.         flag = 1;
  1305.     int    logged;
  1306.     TravInfo ti;
  1307.  
  1308.     while ((tmp = traverse_all_windows(&flag, &ti)) != NULL)
  1309.     {
  1310.         if (!hold_output(tmp) && (stuff = hold_queue(tmp)))
  1311.         {
  1312.             logged = hold_queue_logged(tmp);
  1313.             if (rite(tmp, stuff, 1, 0, 0, logged) == 0)
  1314.             {
  1315.                 remove_from_hold_list(tmp);
  1316.                 hold_flag = 1;
  1317.             }
  1318.         }
  1319.     }
  1320.     return (hold_flag);
  1321. }
  1322.  
  1323. /*
  1324.  * update_window_status: This updates the status line for the given window.
  1325.  * If the refresh flag is true, the entire status line is redrawn.  If not,
  1326.  * only some of the changed portions are redrawn 
  1327.  */
  1328. void
  1329. update_window_status(window, refresh)
  1330.     Window    *window;
  1331.     int    refresh;
  1332. {
  1333.     if (dumb || (!window->visible) || !status_update_flag || never_connected)
  1334.         return;
  1335.     if (window == (Window *) 0)
  1336.         window = curr_scr_win;
  1337.     if (refresh)
  1338.         new_free(&(window->status_line));
  1339.     make_status(window);
  1340. }
  1341.  
  1342. /*
  1343.  * redraw_all_status: This redraws all of the status lines for all of the
  1344.  * windows. 
  1345.  */
  1346. void
  1347. redraw_all_status()
  1348. {
  1349.     Window    *tmp;
  1350.  
  1351.     if (dumb)
  1352.         return;
  1353.     for (tmp = current_screen->window_list; tmp; tmp = tmp->next)
  1354.     {
  1355.         new_free(&(tmp->status_line));
  1356.         make_status(tmp);
  1357.     }
  1358.     update_input(UPDATE_JUST_CURSOR);
  1359.     term_flush();
  1360. }
  1361.  
  1362. /*
  1363.  * update_all_status: This updates all of the status lines for all of the
  1364.  * windows.  By updating, it only draws from changed portions of the status
  1365.  * line to the right edge of the screen 
  1366.  */
  1367. /*ARGSUSED*/
  1368. void
  1369. update_all_status()
  1370. {
  1371.     Window    *window;
  1372.     Screen    *screen;
  1373.  
  1374.     if (dumb || !status_update_flag || never_connected)
  1375.         return;
  1376.     for (screen = screen_list; screen; screen = screen->next)
  1377.     {
  1378.         if (!screen->alive)
  1379.             continue;
  1380.         for (window = screen->window_list;window; window = window->next)
  1381.             if (window->visible)
  1382.                 make_status(window);
  1383.     }
  1384.     update_input(UPDATE_JUST_CURSOR);
  1385.     term_flush();
  1386. }
  1387.  
  1388. /*
  1389.  * status_update: sets the status_update_flag to whatever flag is.  This also
  1390.  * calls update_all_status(), which will update the status line if the flag
  1391.  * was true, otherwise it's just ignored 
  1392.  */
  1393. void
  1394. status_update(flag)
  1395.     int    flag;
  1396. {
  1397.     status_update_flag = flag;
  1398.     update_all_status();
  1399.     cursor_to_input();
  1400. }
  1401.  
  1402. /*
  1403.  * is_current_channel: Returns true is channel is a current channel for any
  1404.  * window.  If the delete flag is set, the unset channel as the current
  1405.  * channel for any and all windows 
  1406.  */
  1407. int
  1408. is_current_channel(channel, delete)
  1409.     char    *channel;
  1410.     char    delete;
  1411. {
  1412.     Window    *tmp;
  1413.     int    found = 0,
  1414.         flag = 1;
  1415.     TravInfo ti;
  1416.  
  1417.     while ((tmp = traverse_all_windows(&flag, &ti)) != NULL)
  1418.     {
  1419.         if (tmp->current_channel &&
  1420.             !my_stricmp(channel, tmp->current_channel) &&
  1421.             (tmp->server == from_server))
  1422.         {
  1423.             found = 1;
  1424.             if (delete)
  1425.             {
  1426.                 new_free(&(tmp->current_channel));
  1427.                 tmp->update |= UPDATE_STATUS;
  1428.             }
  1429.         }
  1430.     }
  1431.     return (found);
  1432. }
  1433.  
  1434. /*
  1435.  * get_window_server: returns the server index for the window with the given
  1436.  * refnum 
  1437.  */
  1438. int    get_window_server(refnum)
  1439. unsigned int    refnum;
  1440. {
  1441.     Window    *tmp;
  1442.  
  1443.     if ((tmp = get_window_by_refnum(refnum)) == (Window *) 0)
  1444.         tmp = curr_scr_win;
  1445.     return (tmp->server);
  1446. }
  1447.  
  1448. /*
  1449.  * set_window_server:  This sets the server of the given window to server. 
  1450.  * If refnum is -1 then we are setting the primary server and all windows
  1451.  * that are set to the current primary server are changed to server.  The all
  1452.  * flag is ignored in this case.  If refnum is not -1, then that window is
  1453.  * set to the given server.  If the all flag is set as well, then all windows
  1454.  * with the same server as renum are set to the new server as well 
  1455.  */
  1456. void
  1457. set_window_server(refnum, server, all)
  1458.     int    refnum;
  1459.     int    server;
  1460.     int    all;
  1461. {
  1462.     Window    *tmp,
  1463.         *window;
  1464.     int    flag = 1,
  1465.         old;
  1466.     TravInfo ti;
  1467.  
  1468.     if (refnum == -1)
  1469.     {
  1470.         while ((tmp = traverse_all_windows(&flag, &ti)) != NULL)
  1471.         {
  1472.             if (tmp->server == primary_server)
  1473.                 tmp->server = server;
  1474.         }
  1475.         window_check_servers();
  1476.         primary_server = server;
  1477.     }
  1478.     else
  1479.     {
  1480.         if ((window = get_window_by_refnum(refnum)) == NULL)
  1481.             window = curr_scr_win;
  1482.         old = window->server;
  1483.         if (all)
  1484.         {
  1485.             while ((tmp = traverse_all_windows(&flag, &ti)) != NULL)
  1486.             {
  1487.                 if (tmp->server == old)
  1488.                     tmp->server = server;
  1489.             }
  1490.         }
  1491.         else
  1492.             window->server = server;
  1493.         flag = 1;
  1494.         window_check_servers();
  1495.     }
  1496. }
  1497.  
  1498. /*
  1499.  * set_channel_by_refnum: This sets the current channel for the current
  1500.  * window. It returns the current channel as it's value.  If channel is null,
  1501.  * the * current channel is not changed, but simply reported by the function
  1502.  * result.  This treats as a special case setting the current channel to
  1503.  * channel "0".  This frees the current_channel for the
  1504.  * current_screen->current_window, * setting it to null 
  1505.  */
  1506. char    *
  1507. set_channel_by_refnum(refnum, channel)
  1508.     unsigned int    refnum;
  1509.     char    *channel;
  1510. {
  1511.     Window    *tmp;
  1512.  
  1513.     if ((tmp = get_window_by_refnum(refnum)) == (Window *) 0)
  1514.         tmp = curr_scr_win;
  1515.     if (channel)
  1516.         if (strcmp(channel, "0") == 0)
  1517.             channel = (char *) 0;
  1518.     malloc_strcpy(&(tmp->current_channel), channel);
  1519.     tmp->waiting_channel = (char *) 0;
  1520.     tmp->update |= UPDATE_STATUS;
  1521.     set_channel_window(tmp, channel);
  1522.     return (channel);
  1523. }
  1524.  
  1525. /* get_channel_by_refnum: returns the current channel for window refnum */
  1526. char    *
  1527. get_channel_by_refnum(refnum)
  1528.     u_int    refnum;
  1529. {
  1530.     Window    *tmp;
  1531.  
  1532.     if ((tmp = get_window_by_refnum(refnum)) == (Window *) 0)
  1533.         tmp = curr_scr_win;
  1534.     return (tmp->current_channel);
  1535. }
  1536.  
  1537. /* current_refnum: returns the reference number for the current window */
  1538. unsigned int
  1539. current_refnum()
  1540. {
  1541.     return (curr_scr_win->refnum);
  1542. }
  1543.  
  1544. /* query_nick: Returns the query nick for the current channel */
  1545. char    *
  1546. query_nick()
  1547. {
  1548.     return (curr_scr_win->query_nick);
  1549. }
  1550.  
  1551. /* get_prompt_by_refnum: returns the prompt for the given window refnum */
  1552. char    *
  1553. get_prompt_by_refnum(refnum)
  1554.     u_int    refnum;
  1555. {
  1556.     Window    *tmp;
  1557.  
  1558.     if ((tmp = get_window_by_refnum(refnum)) == (Window *) 0)
  1559.         tmp = curr_scr_win;
  1560.     if (tmp->prompt)
  1561.         return (tmp->prompt);
  1562.     else
  1563.         return (empty_string);
  1564. }
  1565.  
  1566. /*
  1567.  * get_target_by_refnum: returns the target for the window with the given
  1568.  * refnum (or for the current window).  The target is either the query nick
  1569.  * or current channel for the window 
  1570.  */
  1571. char    *
  1572. get_target_by_refnum(refnum)
  1573.     u_int    refnum;
  1574. {
  1575.     Window    *tmp;
  1576.  
  1577.     if ((tmp = get_window_by_refnum(refnum)) == (Window *) 0)
  1578.         tmp = curr_scr_win;
  1579.     if (tmp->query_nick)
  1580.         return (tmp->query_nick);
  1581.     else if (tmp->current_channel)
  1582.         return (tmp->current_channel);
  1583.     else
  1584.         return ((char *) 0);
  1585. }
  1586.  
  1587. /* set_query_nick: sets the query nick for the current channel to nick */
  1588. void
  1589. set_query_nick(nick)
  1590.     char    *nick;
  1591. {
  1592.     char    *ptr;
  1593.     NickList *tmp;
  1594.  
  1595.     if (curr_scr_win->query_nick)
  1596.     {
  1597.         char    *nick;
  1598.  
  1599.         nick = curr_scr_win->query_nick;
  1600.         while(nick)
  1601.         {
  1602.             if ((ptr = (char *) index(nick,',')) != NULL)
  1603.                 *(ptr++) = '\0';
  1604.             if ((tmp = (NickList *)
  1605.                 remove_from_list(&(curr_scr_win->nicks), nick))
  1606.                 != NULL)
  1607.             {
  1608.                 new_free(&tmp->nick);
  1609.                 new_free(&tmp);
  1610.             }
  1611.             nick = ptr;
  1612.         }
  1613.         new_free(&curr_scr_win->query_nick);
  1614.     }
  1615.     if (nick)
  1616.     {
  1617.         malloc_strcpy(&(curr_scr_win->query_nick), nick);
  1618.         curr_scr_win->update |= UPDATE_STATUS;
  1619.         while (nick)
  1620.         {
  1621.             if ((ptr = (char *) index(nick,',')) != NULL)
  1622.                 *(ptr++) = '\0';
  1623.             tmp = (NickList *) new_malloc(sizeof(NickList));
  1624.             tmp->nick = (char *) 0;
  1625.             malloc_strcpy(&tmp->nick, nick);
  1626.             add_to_list(&(curr_scr_win->nicks),tmp);
  1627.             nick = ptr;
  1628.         }
  1629.     }
  1630.     update_window_status(curr_scr_win,0);
  1631. }
  1632.  
  1633. /*
  1634.  * goto_window: This will switch the current window to the window numbered
  1635.  * "which", where which is 0 through the number of visible windows on the
  1636.  * screen.  The which has nothing to do with the windows refnum. 
  1637.  */
  1638. static    void
  1639. goto_window(which)
  1640.     int    which;
  1641. {
  1642.     Window    *tmp;
  1643.     int    i;
  1644.  
  1645.  
  1646.     if (which == 0)
  1647.         return;
  1648.     if ((which < 0) || (which > current_screen->visible_windows))
  1649.     {
  1650.         say("GOTO: Illegal value");
  1651.         return;
  1652.     }
  1653.     tmp = current_screen->window_list;
  1654.     for (i = 1; tmp && (i != which); tmp = tmp->next, i++)
  1655.         ;
  1656.     set_current_window(tmp);
  1657. }
  1658.  
  1659. /*
  1660.  * hide_window: sets the given window to invisible and recalculates remaing
  1661.  * windows to fill the entire screen 
  1662.  */
  1663. void
  1664. hide_window(window)
  1665.     Window    *window;
  1666. {
  1667.     if (current_screen->visible_windows == 1)
  1668.     {
  1669.         say("You can't hide the last window.");
  1670.         return;
  1671.     }
  1672.     if (window->visible)
  1673.     {
  1674.         remove_from_window_list(window);
  1675.         add_to_invisible_list(window);
  1676. #ifdef SCROLL_AFTER_DISPLAY
  1677.         window->display_size = LI - 3;
  1678. #else
  1679.         window->display_size = LI - 2;
  1680. #endif /* SCROLL_AFTER_DISPLAY */
  1681.         set_current_window((Window *) 0);
  1682.     }
  1683. }
  1684.  
  1685. /* hide_other_windows: makes all visible windows but the current one hidden */
  1686. void
  1687. hide_other_windows()
  1688. {
  1689.     Window    *tmp,
  1690.         *cur,
  1691.         *next;
  1692.  
  1693.     cur = curr_scr_win;
  1694.     tmp = current_screen->window_list;
  1695.     while (tmp)
  1696.     {
  1697.         next = tmp->next;
  1698.         if (tmp != cur)
  1699.             hide_window(tmp);
  1700.         tmp = next;
  1701.     }
  1702. }
  1703.  
  1704. #define WIN_FORM "%%-4s %%-%u.%us %%-%u.%us  %%-%u.%us %%-9.9s %%-10.10s %%s%%s"
  1705. static    void
  1706. list_a_window(window, len)
  1707.     Window    *window;
  1708.     int    len;
  1709. {
  1710.     char    tmp[10];
  1711.  
  1712.     sprintf(tmp, "%-4u", window->refnum);
  1713.     sprintf(buffer, WIN_FORM, NICKNAME_LEN, NICKNAME_LEN, len,
  1714.             len, get_int_var(CHANNEL_NAME_WIDTH_VAR),
  1715.             get_int_var(CHANNEL_NAME_WIDTH_VAR));
  1716.     say(buffer, tmp, get_server_nickname(window->server),
  1717.             window->name?window->name:"<None>",
  1718.             window->current_channel ?
  1719.                 window->current_channel : "<None>",
  1720.             window->query_nick ? window->query_nick : "<None>",
  1721.             get_server_itsname(window->server),
  1722.             bits_to_lastlog_level(window->window_level),
  1723.             (window->visible) ? "" : " Hidden");
  1724. }
  1725.  
  1726. /*
  1727.  * list_windows: This Gives a terse list of all the windows, visible or not,
  1728.  * by displaying their refnums, current channel, and current nick 
  1729.  */
  1730. static    void
  1731. list_windows()
  1732. {
  1733.     Window    *tmp;
  1734.     int    flag = 1;
  1735.     int    len = 4;
  1736.     TravInfo ti;
  1737. #if 0
  1738.     Window    *win;
  1739. #endif
  1740.  
  1741.     while ((tmp = traverse_all_windows(&flag, &ti)) != NULL)
  1742.     {
  1743.         if (tmp->name && (strlen(tmp->name) > len))
  1744.             len = strlen(tmp->name);
  1745.     }
  1746.     sprintf(buffer, WIN_FORM, NICKNAME_LEN, NICKNAME_LEN, len, len,
  1747.         get_int_var(CHANNEL_NAME_WIDTH_VAR),
  1748.         get_int_var(CHANNEL_NAME_WIDTH_VAR));
  1749.     say(buffer, "Ref", "Nick", "Name", "Channel", "Query", "Server",
  1750.         "Level", empty_string);
  1751.     flag = 1;
  1752.     while ((tmp = traverse_all_windows(&flag, &ti)) != NULL)
  1753.         list_a_window(tmp, len);
  1754. #if 0
  1755.     win = NULL
  1756.     while ((new_window_traverse(&win, &flag)) != NULL)
  1757.         list_a_window(win, len);
  1758. #endif
  1759. }
  1760.  
  1761. /* show_window: This makes the given window visible.  */
  1762. static    void
  1763. show_window(window)
  1764.     Window    *window;
  1765. {
  1766.     if (window->visible)
  1767.     {
  1768.         set_current_window(window);
  1769.         return;
  1770.     }
  1771.     remove_from_invisible_list(window);
  1772.     if (add_to_window_list(window))
  1773.         set_current_window(window);
  1774.     else
  1775.         add_to_invisible_list(window);
  1776. }
  1777.  
  1778. /* push_window_by_refnum:  This pushes the given refnum onto the window stack */
  1779. static    void
  1780. push_window_by_refnum(refnum)
  1781.     u_int    refnum;
  1782. {
  1783.     WindowStack *new;
  1784.  
  1785.     new = (WindowStack *) new_malloc(sizeof(WindowStack));
  1786.     new->refnum = refnum;
  1787.     new->next = current_screen->window_stack;
  1788.     current_screen->window_stack = new;
  1789. }
  1790.  
  1791. /*
  1792.  * pop_window: this pops the top entry off the window stack and sets the
  1793.  * current window to that window.  If the top of the window stack is no
  1794.  * longer a valid window, then that entry is discarded and the next entry
  1795.  * down is tried (as so on).  If the stack is empty, the current window is
  1796.  * left unchanged 
  1797.  */
  1798. static    void
  1799. pop_window()
  1800. {
  1801.     int    refnum;
  1802.     WindowStack *tmp;
  1803.     Window    *win;
  1804.  
  1805.     while (1)
  1806.     {
  1807.         if (current_screen->window_stack)
  1808.         {
  1809.             refnum = current_screen->window_stack->refnum;
  1810.             tmp = current_screen->window_stack->next;
  1811.             new_free(¤t_screen->window_stack);
  1812.             current_screen->window_stack = tmp;
  1813.             if ((win = get_window_by_refnum(refnum)) != NULL)
  1814.             {
  1815.                 if (!win->visible)
  1816.                     show_window(win);
  1817.                 else
  1818.                     set_current_window(win);
  1819.                 break;
  1820.             }
  1821.         }
  1822.         else
  1823.         {
  1824.             say("The window stack is empty!");
  1825.             break;
  1826.         }
  1827.     }
  1828. }
  1829.  
  1830. /*
  1831.  * show_stack: displays the current window stack.  This also purges out of
  1832.  * the stack any window refnums that are no longer valid 
  1833.  */
  1834. static    void
  1835. show_stack()
  1836. {
  1837.     WindowStack *last = (WindowStack *) 0,
  1838.         *tmp, *crap;
  1839.     Window    *win;
  1840.     int    flag = 1;
  1841.     int    len = 4;
  1842.     TravInfo ti;
  1843.  
  1844.     while ((win = traverse_all_windows(&flag, &ti)) != NULL)
  1845.     {
  1846.         if (win->name && (strlen(win->name) > len))
  1847.             len = strlen(win->name);
  1848.     }
  1849.     say("Window stack:");
  1850.     tmp = current_screen->window_stack;
  1851.     while (tmp)
  1852.     {
  1853.         if ((win = get_window_by_refnum(tmp->refnum)) != NULL)
  1854.         {
  1855.             list_a_window(win, len);
  1856.             tmp = tmp->next;
  1857.         }
  1858.         else
  1859.         {
  1860.             crap = tmp->next;
  1861.             new_free(&tmp);
  1862.             if (last)
  1863.                 last->next = crap;
  1864.             else
  1865.                 current_screen->window_stack = crap;
  1866.             tmp = crap;
  1867.         }
  1868.     }
  1869. }
  1870.  
  1871. /*
  1872.  * is_window_name_unique: checks the given name vs the names of all the
  1873.  * windows and returns true if the given name is unique, false otherwise 
  1874.  */
  1875. static    int
  1876. is_window_name_unique(name)
  1877.     char    *name;
  1878. {
  1879.     Window    *tmp;
  1880.     int    flag = 1;
  1881.     TravInfo ti;
  1882.  
  1883.     if (name)
  1884.     {
  1885.         while ((tmp = traverse_all_windows(&flag, &ti)) != NULL)
  1886.         {
  1887.             if (tmp->name && (my_stricmp(tmp->name, name) == 0))
  1888.                 return (0);
  1889.         }
  1890.     }
  1891.     return (1);
  1892. }
  1893.  
  1894. /*
  1895.  * add_nicks_by_refnum:  This adds the given str to the nicklist of the
  1896.  * window refnum.  Only unique names are added to the list.  If the name is
  1897.  * preceeded by a ^ it is removed from the list.  The str may be a comma
  1898.  * separated list of nicks, channels, etc 
  1899.  */
  1900. static    void
  1901. add_nicks_by_refnum(refnum, str)
  1902.     u_int    refnum;
  1903.     char    *str;
  1904. {
  1905.     Window    *tmp;
  1906.     char    *ptr;
  1907.     NickList *new;
  1908.  
  1909.     if ((tmp = get_window_by_refnum(refnum)) != NULL)
  1910.     {
  1911.         while (str)
  1912.         {
  1913.             if ((ptr = (char *) index(str, ',')) != NULL)
  1914.                 *(ptr++) = '\0';
  1915.             if (*str == '^')
  1916.             {
  1917.                 if ((new = (NickList *)
  1918.                     remove_from_list(&(tmp->nicks),str + 1)) !=
  1919.                     NULL)
  1920.                 {
  1921.                     say("%s removed from window name list",
  1922.                         new->nick);
  1923.                     new_free(&new->nick);
  1924.                     new_free(&new);
  1925.                 }
  1926.                 else
  1927.                     say("%s is not on the list for this window!", str + 1);
  1928.             }
  1929.             else
  1930.             {
  1931.                 if (!find_in_list(&(tmp->nicks), str,
  1932.                         !USE_WILDCARDS))
  1933.                 {
  1934.                     say("%s add to window name list", str);
  1935.                     new = (NickList *)
  1936.                         new_malloc(sizeof(NickList));
  1937.                     new->nick = (char *) 0;
  1938.                     malloc_strcpy(&new->nick, str);
  1939.                     add_to_list(&(tmp->nicks), new);
  1940.                 }
  1941.                 else
  1942.                     say("%s already on window name list",
  1943.                         str);
  1944.             }
  1945.             str = ptr;
  1946.         }
  1947.     }
  1948.     else
  1949.         say("No such window!");
  1950. }
  1951.  
  1952. /* below is stuff used for parsing of WINDOW command */
  1953.  
  1954. /*
  1955.  * get_window_by_name: returns a pointer to a window with a matching logical
  1956.  * name or null if no window matches 
  1957.  */
  1958. Window    *
  1959. get_window_by_name(name)
  1960.     char    *name;
  1961. {
  1962.     Window    *tmp;
  1963.     int    flag = 1;
  1964.     TravInfo ti;
  1965.  
  1966.     while ((tmp = traverse_all_windows(&flag, &ti)) != NULL)
  1967.     {
  1968.         if (tmp->name && (my_stricmp(tmp->name, name) == 0))
  1969.             return (tmp);
  1970.     }
  1971.     return ((Window *) 0);
  1972. }
  1973.  
  1974. /*
  1975.  * get_window: this parses out any window (visible or not) and returns a
  1976.  * pointer to it 
  1977.  */
  1978. static    Window    *
  1979. get_window(name, args)
  1980.     char    *name;
  1981.     char    **args;
  1982. {
  1983.     char    *arg;
  1984.     Window    *tmp;
  1985.  
  1986.     if ((arg = next_arg(*args, args)) != NULL)
  1987.     {
  1988.         if (is_number(arg))
  1989.         {
  1990.             if ((tmp = get_window_by_refnum(atoi(arg))) != NULL)
  1991.                 return (tmp);
  1992.         }
  1993.         if ((tmp = get_window_by_name(arg)) != NULL)
  1994.             return (tmp);
  1995.         say("%s: No such window: %s", name, arg);
  1996.     }
  1997.     else
  1998.         say("%s: Please specify a window refnum or name", name);
  1999.     return ((Window *) 0);
  2000. }
  2001.  
  2002. /*
  2003.  * get_invisible_window: parses out an invisible window by reference number.
  2004.  * Returns the pointer to the window, or null.  The args can also be "LAST"
  2005.  * indicating the top of the invisible window list (and thus the last window
  2006.  * made invisible) 
  2007.  */
  2008. static    Window    *
  2009. get_invisible_window(name, args)
  2010.     char    *name;
  2011.     char    **args;
  2012. {
  2013.     char    *arg;
  2014.     Window    *tmp;
  2015.  
  2016.     if ((arg = next_arg(*args, args)) != NULL)
  2017.     {
  2018.         if (my_strnicmp(arg, "LAST", strlen(arg)) == 0)
  2019.         {
  2020.             if (invisible_list == (Window *) 0)
  2021.                 say("%s: There are no hidden windows", name);
  2022.             return (invisible_list);
  2023.         }
  2024.         if ((tmp = get_window(name, &arg)) != NULL)
  2025.         {
  2026.             if (!tmp->visible)
  2027.                 return (tmp);
  2028.             else
  2029.             {
  2030.                 if (tmp->name)
  2031.                     say("%s: Window %s is not hidden!",
  2032.                         name, tmp->name);
  2033.                 else
  2034.                     say("%s: Window %d is not hidden!",
  2035.                         name, tmp->refnum);
  2036.             }
  2037.         }
  2038.     }
  2039.     else
  2040.         say("%s: Please specify a window refnum or LAST", name);
  2041.     return ((Window *) 0);
  2042. }
  2043.  
  2044. #ifdef THIS_NEVER_GETS_USED
  2045. /*
  2046.  * get_visible_window: parses out a window by reference number.  Returns the
  2047.  * pointer to thw window if it exists and if it is visible. 
  2048.  */
  2049. static    Window    *
  2050. get_visible_window(name, args)
  2051.     char    *name;
  2052.     char    **args;
  2053. {
  2054.     char    *arg;
  2055.     Window    *tmp;
  2056.  
  2057.     if (arg = next_arg(*args, args))
  2058.     {
  2059.         if (my_strnicmp(arg, "LAST", strlen(arg)) == 0)
  2060.             tmp = get_window_by_refnum(current_screen->last_window_refnum);
  2061.         else
  2062.             tmp = get_window(name, &arg);
  2063.         if (tmp)
  2064.         {
  2065.             if (tmp->visible)
  2066.                 return (tmp);
  2067.             else
  2068.             {
  2069.                 if (tmp->name)
  2070.                     say("%s: Window %s is hidden!", name, tmp->name);
  2071.                 else
  2072.                     say("%s: Window %d is hidden!", name, tmp->refnum);
  2073.             }
  2074.         }
  2075.     }
  2076.     else
  2077.         say("%s: Please specify a window refnum or LAST", name);
  2078.     return ((Window *) 0);
  2079. }
  2080. #endif
  2081.  
  2082. /* get_number: parses out an integer number and returns it */
  2083. static    int
  2084. get_number(name, args)
  2085.     char    *name;
  2086.     char    **args;
  2087. {
  2088.     char    *arg;
  2089.  
  2090.     if ((arg = next_arg(*args, args)) != NULL)
  2091.         return (atoi(arg));
  2092.     else
  2093.         say("%s: You must specify the number of lines", name);
  2094.     return (0);
  2095. }
  2096.  
  2097. /*
  2098.  * get_boolean: parses either ON, OFF, or TOGGLE and sets the var
  2099.  * accordingly.  Returns 0 if all went well, -1 if a bogus or missing value
  2100.  * was specified 
  2101.  */
  2102. static    int
  2103. get_boolean(name, args, var)
  2104.     char    *name;
  2105.     char    **args;
  2106.     int    *var;
  2107. {
  2108.     char    *arg;
  2109.  
  2110.     if (((arg = next_arg(*args, args)) == (char *) 0) ||
  2111.         do_boolean(arg, var))
  2112.     {
  2113.         say("Value for %s must be ON, OFF, or TOGGLE", name);
  2114.         return (-1);
  2115.     }
  2116.     else
  2117.     {
  2118.         say("Window %s is %s", name, var_settings[*var]);
  2119.         return (0);
  2120.     }
  2121. }
  2122.  
  2123. /*ARGSUSED*/
  2124. void
  2125. window(command, args)
  2126.     char    *command,
  2127.         *args;
  2128. {
  2129.     int    len;
  2130.     char    *arg;
  2131.     int    no_args = 1;
  2132.     Window    *window,
  2133.         *tmp;
  2134.  
  2135.     in_window_command = 1;
  2136.     message_from((char *) 0, LOG_CURRENT);
  2137.     window = curr_scr_win;
  2138.     while ((arg = next_arg(args, &args)) != NULL)
  2139.     {
  2140.         no_args = 0;
  2141.         len = strlen(arg);
  2142.         if (my_strnicmp("NEW", arg, len) == 0)
  2143.         {
  2144.             if ((tmp = new_window()) != NULL)
  2145.                 window = tmp;
  2146.         }
  2147. #ifdef WINDOW_CREATE
  2148.         else if (my_strnicmp("CREATE", arg, len) == 0)
  2149.         {
  2150.             if ((tmp = create_additional_screen()) != NULL)
  2151.                 window = tmp;
  2152.             else
  2153.                 say("Cannot create new screen!");
  2154.         }
  2155.         else if (!my_strnicmp("DELETE", arg, len))
  2156.             kill_screen(current_screen);
  2157. #endif /* WINODW_CREATE */
  2158.         else if (my_strnicmp("REFNUM", arg, len) == 0)
  2159.         {
  2160.             if ((tmp = get_window("REFNUM", &args)) != NULL)
  2161.             {
  2162.                 if (tmp->screen && tmp->screen !=window->screen)
  2163.                     say("Window in another screen!");
  2164.                 else if (tmp->visible)
  2165.                 { 
  2166.                     set_current_window(tmp);
  2167.                     window = tmp;
  2168.                 }
  2169.                 else
  2170.                     say("Window not visible!");
  2171.             }
  2172.             else
  2173.             {
  2174.                 say("No such window!");
  2175.                 in_window_command = 0;
  2176.                 return;
  2177.             }
  2178.         }
  2179.         else if (my_strnicmp("KILL", arg, len) == 0)
  2180.             delete_window(window);
  2181.         else if (my_strnicmp("SHRINK", arg, len) == 0)
  2182.             grow_window(window, -get_number("SHRINK", &args));
  2183.         else if (my_strnicmp("GROW", arg, len) == 0)
  2184.             grow_window(window, get_number("SHRINK", &args));
  2185.         else if (my_strnicmp("SCROLL", arg, len) == 0)
  2186.             get_boolean("SCROLL", &args, &(window->scroll));
  2187.         else if (my_strnicmp("LOG", arg, len) == 0)
  2188.         {
  2189.             if (get_boolean("LOG", &args, &(window->log)))
  2190.                 break;
  2191.             else
  2192.             {
  2193.                 char    *logfile;
  2194.                 int    add_ext = 1;
  2195.  
  2196.                 if ((logfile = window->logfile) != NULL)
  2197.                     add_ext = 0;
  2198.                 else if (!(logfile = get_string_var(LOGFILE_VAR)))
  2199.                     logfile = empty_string;
  2200.                 if (!add_ext)
  2201.                     sprintf(buffer, "%s", logfile);
  2202.                 else if (window->current_channel)
  2203.                     sprintf(buffer, "%s.%s", logfile,
  2204.                         window->current_channel);
  2205.                 else if (window->query_nick)
  2206.                     sprintf(buffer, "%s.%s", logfile,
  2207.                         window->query_nick);
  2208.                 else
  2209.                     sprintf(buffer, "%s.Window_%d", logfile,
  2210.                         window->refnum);
  2211.                 window->log_fp = do_log(window->log, buffer,
  2212.                     window->log_fp);
  2213.                 if (window->log_fp == (FILE *) 0)
  2214.                     window->log = 0;
  2215.             }
  2216.         }
  2217.         else if (my_strnicmp("HOLD_MODE", arg, len) == 0)
  2218.         {
  2219.             if (get_boolean("HOLD_MODE",&args,&(window->hold_mode)))
  2220.                 break;
  2221.             else
  2222.                 set_int_var(HOLD_MODE_VAR, window->hold_mode);
  2223.         }
  2224.         else if (my_strnicmp("LASTLOG_LEVEL", arg, len) == 0)
  2225.         {
  2226.             if ((arg = next_arg(args, &args)) != NULL)
  2227.             {
  2228.                 window->lastlog_level =parse_lastlog_level(arg);
  2229.                 say("Lastlog level is %s",
  2230.                   bits_to_lastlog_level(window->lastlog_level));
  2231.             }
  2232.             else
  2233.                 say("Level required");
  2234.         }
  2235.         else if (my_strnicmp("LEVEL", arg, len) == 0)
  2236.         {
  2237.             if ((arg = next_arg(args, &args)) != NULL)
  2238.             {
  2239.                 window->window_level = parse_lastlog_level(arg);
  2240.                 say("Window level is %s",
  2241.                    bits_to_lastlog_level(window->window_level));
  2242.                 revamp_window_levels(window);
  2243.             }
  2244.             else
  2245.                 say("LEVEL: Level required");
  2246.         }
  2247.         else if (my_strnicmp("BALANCE", arg, len) == 0)
  2248.             recalculate_windows();
  2249. #ifdef _Windows
  2250.         else if (my_strnicmp("TITLE", arg, len) == 0)
  2251.         {
  2252.             if ((arg = next_arg(args, &args)) != NULL)
  2253.             {
  2254.                 if (window->visible && window->screen)
  2255.                 {
  2256.                     SetWindowText(window->screen->hwnd, arg);
  2257.                 }
  2258.                 else
  2259.                 {
  2260.                     say("The current window is not attached to a screen");
  2261.                 }
  2262.             }
  2263.             else
  2264.             {
  2265.                 say("You must specify a name for the window!");
  2266.             }
  2267.         }
  2268. #endif
  2269.         else if (my_strnicmp("NAME", arg, len) == 0)
  2270.         {
  2271.             if ((arg = next_arg(args, &args)) != NULL)
  2272.             {
  2273.                 if (is_window_name_unique(arg))
  2274.                 {
  2275.                     malloc_strcpy(&(window->name), arg);
  2276.                     window->update |= UPDATE_STATUS;
  2277.                 }
  2278.                 else
  2279.                     say("%s is not unique!", arg);
  2280.             }
  2281.             else
  2282.                 say("You must specify a name for the window!");
  2283.         }
  2284.         else if (my_strnicmp("PROMPT", arg, len) == 0)
  2285.         {
  2286.             if ((arg = next_arg(args, &args)) != NULL)
  2287.             {
  2288.                 malloc_strcpy(&(window->prompt), arg);
  2289.                 window->update |= UPDATE_STATUS;
  2290.             }
  2291.             else
  2292.                 say("You must specify a prompt for the window!");
  2293.         }
  2294.         else if (my_strnicmp("GOTO", arg, len) == 0)
  2295.         {
  2296.             goto_window(get_number("GOTO", &args));
  2297.             window = curr_scr_win;
  2298.         }
  2299.         else if (my_strnicmp("LAST", arg, len) == 0)
  2300.             set_current_window((Window *) 0);
  2301.         else if (my_strnicmp("MOVE", arg, len) == 0)
  2302.         {
  2303.             move_window(window, get_number("MOVE", &args));
  2304.             window = curr_scr_win;
  2305.         }
  2306.         else if (my_strnicmp("SWAP", arg, len) == 0)
  2307.         {
  2308.             if ((tmp = get_invisible_window("SWAP", &args)) != NULL)
  2309.                 swap_window(window, tmp);
  2310.         }
  2311.         else if (my_strnicmp("HIDE", arg, len) == 0)
  2312.             hide_window(window);
  2313.         else if (my_strnicmp("PUSH", arg, len) == 0)
  2314.             push_window_by_refnum(window->refnum);
  2315.         else if (my_strnicmp("POP", arg, len) == 0)
  2316.             pop_window();
  2317.         else if (my_strnicmp("ADD", arg, len) == 0)
  2318.         {
  2319.             if ((arg = next_arg(args, &args)) != NULL)
  2320.             {
  2321.                 add_nicks_by_refnum(window->refnum, arg);
  2322.             }
  2323.             else
  2324.                 say("ADD: Do something!  Geez!");
  2325.         }
  2326.         else if (my_strnicmp("REMOVE", arg, len) == 0)
  2327.         {
  2328.             if ((arg = next_arg(args, &args)) != NULL)
  2329.             {
  2330.                 char    *tmp = (char *) malloc(strlen(arg) + 1);
  2331.  
  2332.                 *tmp = '^';
  2333.                 strcpy(tmp + 1, arg);
  2334.                 add_nicks_by_refnum(window->refnum, tmp);
  2335.                 new_free(&tmp);
  2336.             }
  2337.             else
  2338.                 say("REMOVE: Do something!  Geez!");
  2339.         }
  2340.         else if (my_strnicmp("STACK", arg, len) == 0)
  2341.             show_stack();
  2342.         else if (my_strnicmp("LIST", arg, len) == 0)
  2343.             list_windows();
  2344.         else if (my_strnicmp("SERVER", arg, len) == 0)
  2345.         {
  2346.             if ((arg = next_arg(args, &args)) != NULL)
  2347.             {
  2348.                 int    i,
  2349.                     port_num;
  2350.                 char    *port,
  2351.                     *password,
  2352.                     *nick;
  2353.  
  2354.                 parse_server_info(arg, &port, &password, &nick);
  2355.                 if (port)
  2356.                     port_num = atoi(port);
  2357.                 else
  2358.                     port_num = -1;
  2359.                 if ((i = find_in_server_list(arg, port_num))
  2360.                         != -1)
  2361.                     port_num = server_list[i].port;
  2362.                 if (-1 == (i = parse_server_index(arg)))
  2363.                 {
  2364.                     if (nick && *nick)
  2365.                         malloc_strcpy(&connect_next_nick,
  2366.                         nick);
  2367.                     if (!connect_to_server(arg,port_num,-1))
  2368.                     {
  2369.                         set_window_server(window->refnum,
  2370.                         from_server, 0);
  2371. #ifndef PHONE
  2372.                         window->window_level = LOG_ALL;
  2373. #else
  2374.                         window->window_level = LOG_NONE;
  2375. #endif
  2376.                         clear_channel_list(from_server);
  2377.                         if (window->current_channel)
  2378.                           new_free(&window->current_channel);
  2379.                     }
  2380.                 }
  2381.                 else
  2382.                 {
  2383.                     if (nick && *nick)
  2384.                         malloc_strcpy(&(
  2385.                             server_list[i].nickname),
  2386.                             nick);
  2387.                     if(!connect_to_server(get_server_name(i)
  2388.                         , server_list[i].port, -1))
  2389.                     {
  2390.                         set_window_server(window->refnum,
  2391.                         from_server, 0);
  2392. #ifndef PHONE
  2393.                         window->window_level = LOG_ALL;
  2394. #else
  2395.                         window->window_level = LOG_NONE;
  2396. #endif
  2397.                         if (window->current_channel)
  2398.                           new_free(&window->current_channel);
  2399.                     }
  2400.                 }
  2401.             }
  2402.             else
  2403.                 say("SERVER: You must specify a server");
  2404.         }
  2405.         else if (my_strnicmp("SHOW", arg, len) == 0)
  2406.         {
  2407.             if ((tmp = get_window("SHOW", &args)) != NULL)
  2408.             {
  2409.                 show_window(tmp);
  2410.                 window = curr_scr_win;
  2411.             }
  2412.         }
  2413.         else if (my_strnicmp("HIDE_OTHERS", arg, len) == 0)
  2414.             hide_other_windows();
  2415.         else if (my_strnicmp("KILL_OTHERS", arg, len) == 0)
  2416.             delete_other_windows();
  2417.         else if (my_strnicmp("NOTIFY", arg, len) == 0)
  2418.         {
  2419.             window->miscflags ^= WINDOW_NOTIFY;
  2420.             say("Notification when hidden set to %s",
  2421.                 (window->miscflags & WINDOW_NOTIFY)? "ON" : "OFF");
  2422.         }
  2423.         else if (my_strnicmp("CHANNEL", arg, len) == 0)
  2424.         {
  2425.             if ((arg = next_arg(args, &args)) != NULL)
  2426.             {
  2427.                 if (is_current_channel(arg, 1))
  2428.                 {
  2429.                     say("You are now talking to channel %s",
  2430.                         arg);
  2431.                     set_channel_by_refnum(0, arg);
  2432.                 }
  2433.                 else
  2434.                 {
  2435.             if (is_on_channel(arg, nickname))
  2436.             {
  2437.                 say("You are now talking to channel %s", arg);
  2438.                 set_channel_by_refnum(0, arg);
  2439.             }
  2440.             else if (*arg == '0' && !*(args + 1))
  2441.                 set_channel_by_refnum(0, NULL);
  2442.             else
  2443.             {
  2444.                 int    server;
  2445.  
  2446.                 server = from_server;
  2447.                 from_server = window->server;
  2448.                 if(get_server_version(window->server)>Server2_5)
  2449.                     send_to_server("JOIN %s", arg);
  2450.                 else
  2451.                     send_to_server( "CHANNEL %s", arg);
  2452.                 malloc_strcpy(&window->waiting_channel, arg);
  2453.                 from_server = server;
  2454.             }
  2455.                 }
  2456.             }
  2457.             else
  2458.                 set_channel_by_refnum(0, "0");
  2459.         }
  2460.         else if (my_strnicmp("PREVIOUS", arg, len) == 0)
  2461.         {
  2462.             swap_previous_window();
  2463.         }
  2464.         else if (my_strnicmp("NEXT", arg, len) == 0)
  2465.         {
  2466.             swap_next_window();
  2467.         }
  2468.         else if (my_strnicmp("BACK", arg, len) == 0)
  2469.         {
  2470.             back_window();
  2471.         }
  2472.         else if (my_strnicmp("KILLSWAP", arg, len) == 0)
  2473.         {
  2474.             window_kill_swap();
  2475.         }
  2476.         else if (!my_strnicmp("LOGFILE", arg, len))
  2477.         {
  2478.             if ((arg = next_arg(args, &args)) != NULL)
  2479.             {
  2480.                 malloc_strcpy(&window->logfile, arg);
  2481.                 say("Window LOGFILE set to %s", arg);
  2482.             }
  2483.             else
  2484.                 say("No LOGFILE given");
  2485.         }
  2486.         else if (!my_strnicmp("NOTIFY_LEVEL", arg, len))
  2487.         {
  2488.             if ((arg = next_arg(args, &args)) != NULL)
  2489.             {
  2490.                 window->notify_level = parse_lastlog_level(arg);
  2491.                 say("Window notify level is %s",
  2492.                    bits_to_lastlog_level(window->notify_level));
  2493.             }
  2494.             else
  2495.                 say("Level missing");
  2496.         }
  2497.         else if (!my_strnicmp("NUMBER", arg, len))
  2498.         {
  2499.             if ((arg = next_arg(args, &args)) != (char *) 0)
  2500.             {
  2501.                 int    i;
  2502.                 Window    *tmp;
  2503.  
  2504.                 i = atoi(arg);
  2505.                 if (i > 0)
  2506.                 {
  2507.                     /* check if window number exists */
  2508.                         
  2509.                     tmp = get_window_by_refnum(i);
  2510.                     if (!tmp)
  2511.                         window->refnum = i;
  2512.                     else
  2513.                     {
  2514.                         tmp->refnum = window->refnum;
  2515.                         window->refnum = i;
  2516.                     }
  2517.                     update_all_status();
  2518.                 }
  2519.                 else
  2520.                     say("Window number must be greater than 1");
  2521.             }
  2522.             else
  2523.                 say("Window number missing");
  2524.         }
  2525.         else
  2526.             say("Unknown WINDOW command: %s", arg);
  2527.     }
  2528.     if (no_args)
  2529.     {
  2530.         if (window->name)
  2531.             say("Window %s (%u)", window->name, window->refnum);
  2532.         else
  2533.             say("Window %u", window->refnum);
  2534.         if (window->server == -1)
  2535.             say("\tServer: <None>");
  2536.         else
  2537.             say("\tServer: %s", get_server_name(window->server));
  2538.         say("\tCurrent channel: %s", window->current_channel ?
  2539.             window->current_channel : "<None>");
  2540.         say("\tQuery User: %s", (window->query_nick ?
  2541.             window->query_nick : "<None>"));
  2542.         say("\tPrompt: %s", window->prompt ?
  2543.             window->prompt : "<None>");
  2544.         say("\tScrolling is %s", var_settings[window->scroll]);
  2545.         say("\tLogging is %s", var_settings[window->log]);
  2546.         if (window->logfile)
  2547.             say("\tLogfile is %s", window->logfile);
  2548.         else
  2549.             say("\tNo logfile given");
  2550.         say("\tNotification is %s",
  2551.             var_settings[window->miscflags & WINDOW_NOTIFY]);
  2552.         say("\tHold mode is %s", var_settings[window->hold_mode]);
  2553.         say("\tWindow level is %s",
  2554.             bits_to_lastlog_level(window->window_level));
  2555.         say("\tLastlog level is %s",
  2556.             bits_to_lastlog_level(window->lastlog_level));
  2557.         say("\tNotify level is %s",
  2558.             bits_to_lastlog_level(window->notify_level));
  2559.         if (window->nicks)
  2560.         {
  2561.             NickList *tmp;
  2562.  
  2563.             say("\tName list:");
  2564.             for (tmp = window->nicks; tmp; tmp = tmp->next)
  2565.                 say("\t  %s", tmp->nick);
  2566.         }
  2567.     }
  2568.     in_window_command = 0;
  2569.     message_from((char *) 0, LOG_CRAP);
  2570.     update_all_windows();
  2571.     cursor_to_input();
  2572. }
  2573.  
  2574. int
  2575. number_of_windows()
  2576. {
  2577.     return (current_screen->visible_windows);
  2578. }
  2579.  
  2580. void
  2581. unstop_all_windows()
  2582. {
  2583.     Window    *tmp;
  2584.  
  2585.     for (tmp = current_screen->window_list; tmp; tmp = tmp->next)
  2586.         hold_mode(tmp, OFF, 1);
  2587. }
  2588.  
  2589. /* this will make underline toggle between 2 and -1 and never let it get to 0 */
  2590. void
  2591. set_underline_video(value)
  2592. int    value;
  2593. {
  2594.     if (value == OFF)
  2595.         underline = -1;
  2596.     else
  2597.         underline = 1;
  2598. }
  2599.  
  2600.